package com.iterative.mysql.util;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DownloadServlet extends HttpServlet{
    static final private String CONTENT_TYPE = "text/html; charset=GBK";

    //Initialize global variables
    public void init() throws ServletException {
    }

    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html; charset=gb2312");

        HttpSession session = request.getSession();

        //要下载的文件名
        //之所以这样处理，主要是因为文件名的中文化问题，这样处理的话，中文文件名也会正常显示
        String downloadfile = new String((request.getParameter("downFile")).getBytes(
                    "iso8859-1"), "gb2312");

        ServletContext context = getServletContext();
        ServletConfig config = getServletConfig();

        //获取要下载文件所在的目录，这里是对应于服务器上的物理路径
        //目录的格式是这样的:
        // 根目录（WEB主目录所对应的实际物理目录）
        //+虚拟目录（下载文件存放的子目录）
        String downloadpath = context.getRealPath(File.separator) +
            config.getInitParameter("downloadPath") + File.separator;

        //构建下载文件的对象
        java.io.File file = new java.io.File(downloadpath + downloadfile);

        //获得文件的长度
        long filesize = file.length();
        //设置输出格式
        response.addHeader("content-type", "application/x-msdownload;");
        response.addHeader("Content-Disposition",
            "attachment; filename=" + response.encodeURL(downloadfile));
        response.addHeader("content-length", Long.toString(filesize));

        //向客户端写入文件
        java.io.FileInputStream fin = new java.io.FileInputStream(file);
        byte[] b = new byte[1];
        int j = 0;

        while ((j = fin.read(b)) > 0) {
            response.getOutputStream().write(b);
        }

        fin.close();
    }

    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    }

    //Clean up resources
    public void destroy() {
    }
}
